package com.site.blog.my.core.controller.admin;

import com.site.blog.my.core.util.QiniuUtils;
import com.site.blog.my.core.util.Result;
import com.site.blog.my.core.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author yangshare
 * @link https://yangshare.goho.co/
 */
@Controller
@RequestMapping("/admin")
public class UploadController {
    @Autowired
    private QiniuUtils qiniuUtils;

    /**
     * 附件上传
     *
     * @param file 附件
     * @return 返回数据
     */
    @PostMapping({"/upload/file"})
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file) {
        try {
            String fileName = qiniuUtils.storage(file.getInputStream());
            Result resultSuccess = ResultGenerator.genSuccessResult();
            resultSuccess.setData("http://qiuniu.yangshare.com/" + fileName);
            return resultSuccess;
        } catch (IOException e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("文件上传失败");
        }
    }

}
